package com.ts.main.admin.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ts.main.admin.context.UserContext;
import com.ts.main.admin.dto.OrderDto;
import com.ts.main.admin.dto.OrderSearchPageDto;
import com.ts.main.admin.entity.OrderEntity;
import com.ts.main.admin.enums.OrderStatusEnum;
import com.ts.main.admin.exception.CommonException;
import com.ts.main.admin.exception.RPage;
import com.ts.main.admin.mapper.OrderMapper;
import com.ts.main.admin.vo.AdminRoleVo;
import com.ts.main.admin.vo.OrderVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author lk
* @description 针对表【t_order(用户订单表)】的数据库操作Service实现
* @createDate 2025-06-09 00:56:13
*/
@AllArgsConstructor
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity>{

    public RPage<List<OrderVo>> getOrderPage(OrderSearchPageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<OrderVo> list = baseMapper.getOrderPage(dto);
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public OrderVo getOrderDetails(Integer orderId) {
        return baseMapper.getOrderDetails(orderId);
    }

    public Boolean updateOrder(OrderDto dto) {
        dto.setDealUserId(UserContext.getUser().getId());
        OrderEntity orderEntity = getById(dto.getOrderId());
        if (!orderEntity.getOrderStatus().equals(OrderStatusEnum.WAIT_DELIVER.getTypeValue()))
            throw new CommonException("订单状态异常");
        BeanUtils.copyProperties(dto, orderEntity);
        //修改订单状态（已发货）
        orderEntity.setOrderStatus(OrderStatusEnum.DELIVER.getTypeValue());
        orderEntity.setOrderSort(OrderStatusEnum.DELIVER.getTypeOrder());
        return updateById(orderEntity);
    }

}




